<?php
namespace app\service;

/**
 * 海报工具类
 * @Author luyu <595080590@qq.com>
 * @Date 2019/10/25
 * Class PosterService
 * @package app\common\service
 */
class PosterService
{

    /**
     * 获取位置数据
     * @Author luyu <595080590@qq.com>
     * @Date 2019/10/25
     * @param $data
     * @return mixed
     */
    public function getRealData($data)
    {

        $data['left'] = intval(str_replace('px', '', $data['left'])) * 2;
        $data['top'] = intval(str_replace('px', '', $data['top'])) * 2;
        $data['width'] = intval(str_replace('px', '', $data['width'])) * 2;
        $data['height'] = intval(str_replace('px', '', $data['height'])) * 2;
        if (isset($data['size'])){
            $data['size'] = intval(str_replace('px', '', $data['size'])) * 2;
        }
        if (isset($data['src'])){
            $data['src'] = $data['src'];
        }
        return $data;
    }


    /**
     * 合并图片
     * @Author luyu <595080590@qq.com>
     * @Date 2019/10/25
     * @param $target
     * @param $data
     * @param $imgurl
     * @return mixed
     */
    public function mergeImage($target, $data, $imgurl)
    {
        $img = $this->createImage($imgurl);
        $w = imagesx($img);
        $h = imagesy($img);
        imagecopyresized($target, $img, $data['left'], $data['top'], 0, 0, $data['width'], $data['height'], $w, $h);
        imagedestroy($img);
        return $target;
    }


    /**
     * 创建一块画布，并从字符串中的图像流新建一副图像
     * @Author luyu <595080590@qq.com>
     * @Date 2019/10/25
     * @param $imgurl
     * @return resource|string
     */
    public function createImage($imgurl)
    {
        $resp = @file_get_contents($imgurl);
        if ($resp) {
            return imagecreatefromstring($resp);
        }

        $i = 0;

        while ($i < 3) {
            $resp = @file_get_contents($imgurl);
            if ($resp) {
                return imagecreatefromstring($resp);
            }

            ++$i;
        }

        return '';
    }

    /**
     * 写入文字
     * @Author luyu <595080590@qq.com>
     * @Date 2019/10/25
     * @param $target
     * @param $data
     * @param $text
     * @return mixed
     */
    public function mergeText($target, $data, $text)
    {
        $font = ROOT_PATH . '/public/static/fonts/msyh.ttf';
        $colors = $this->hex2rgb($data['color']);
        $color = imagecolorallocate($target, $colors['red'], $colors['green'], $colors['blue']);
        imagettftext($target, $data['size'], 0, $data['left'], $data['top'] + $data['size'], $color, $font, $text);
        return $target;
    }

    /**
     * 字体颜色
     * @Author luyu <595080590@qq.com>
     * @Date 2019/10/25
     * @param $colour
     * @return array|bool
     */
    public function hex2rgb($colour)
    {
        if ($colour[0] == '#') {
            $colour = substr($colour, 1);
        }

        if (strlen($colour) == 6) {
            list($r, $g, $b) = array($colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5]);
        }
        else if (strlen($colour) == 3) {
            list($r, $g, $b) = array($colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2]);
        }
        else {
            return false;
        }

        $r = hexdec($r);
        $g = hexdec($g);
        $b = hexdec($b);
        return array('red' => $r, 'green' => $g, 'blue' => $b);
    }


    /**
     * 创建生成画布
     * @Author luyu <595080590@qq.com>
     * @Date 2019/10/25
     * @param $poster
     * @param $member
     * @param $qr
     * @param bool $upload
     */
    public function createPoster($poster, $member, $qr, $upload = true)
    {
        $path = ROOT_PATH . '/public/static/poster/';

        if (!is_dir($path)) {
            mkdir($path);
        }
//        $md5 = md5(json_encode(array('uid' => $member['id'], 'bg' => $poster['bg'], 'data' => $poster['data'])));
        $file = $member['id'] . '.png';
        if (!is_file($path . $file)) {
            set_time_limit(0);
            @ini_set('memory_limit', '256M');
            $target = imagecreatetruecolor(640, 1008);
            $bg = $this->createImage(request()->domain().'/'.$poster['bg']);
            imagecopy($target, $bg, 0, 0, 0, 0, 640, 1008);
            imagedestroy($bg);
            $data = json_decode(str_replace('&quot;', '\'', $poster['data']), true);

            foreach ($data as $d) {
                $d = $this->getRealData($d);

                if ($d['type'] == 'head') {
                    $avatar = preg_replace('/\\/0$/i', '/96', $member['avatar']?$member['avatar']:request()->domain().'/static/weixin/img/head.png');
                    $target = $this->mergeImage($target, $d, $avatar);
                }
                else if ($d['type'] == 'img') {
                    $target = $this->mergeImage($target, $d, request()->domain().'/'.$d['src']);
                }
                else if ($d['type'] == 'qr') {
                    $target = $this->mergeImage($target, $d, $qr['current_qrimg']);
                }
                else if ($d['type'] == 'nickname') {
                    $target = $this->mergeText($target, $d, $member['nickname']?$member['nickname']:$member['username']);
                }

            }
            ob_start();
            imagepng($target, $path . $file);
            imagedestroy($target);
            ob_end_clean();
        }


    }




}